﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvvmOrm.Models.DatabaseItem
{
    partial class Row
    {
        partial void PostInitialize()
        {
            Child_DatabaseItemLogs_OrderBy = new List<DatabaseItemLog.Param>();
            Child_DatabaseItemLogs_OrderBy.Add(new DatabaseItemLog.Param(DatabaseItemLog.Field.Id));
        }

        public DatabaseItemTypeEnum TypeEnum
        {
            get { return (DatabaseItemTypeEnum)Type; }
            set
            {
                if (value == (DatabaseItemTypeEnum)Type)
                    return;
                Type = (int)value;
                RaisePropertyChanged(() => TypeEnum);
            }
        }

        public string NameSanitized
        {
            get { return MainViewModel.SanitizeName(_Name); ; }
        }

        public string QualifiedNameSanitized
        {
            get { return Parent_DatabaseModel.Namespace + "." + (Schema == null ? "" : MainViewModel.SanitizeName(Schema) + ".") + NameSanitized; }
        }

        public IEnumerable<Column.Row> PrimaryKeys
        {
            get { return Child_Columns.Where(c => c.IsPrimaryKey); }
        }

        public IEnumerable<Column.Row> NotReadOnly
        {
            get { return Child_Columns.Where(c => !c.IsReadOnly); }
        }

        public Column.Row AutoIncrement
        {
            get { return Child_Columns.FirstOrDefault(c => c.IsAutoIncrement); }
        }

        public void LogInfo(string message)
        {
            Child_DatabaseItemLogs.Add(new DatabaseItemLog.Row(0, 0, (int)DatabaseItemLogTypeEnum.Info, message));
        }
        public void LogWarning(string message)
        {
            Child_DatabaseItemLogs.Add(new DatabaseItemLog.Row(0, 0, (int)DatabaseItemLogTypeEnum.Warning, message));
        }
        public void LogError(string message)
        {
            Child_DatabaseItemLogs.Add(new DatabaseItemLog.Row(0, 0, (int)DatabaseItemLogTypeEnum.Error, message));
        }

        public List<string> LogErrors
        {
            get
            {
                return Child_DatabaseItemLogs.Where(_ => _.MessageType == (int)DatabaseItemLogTypeEnum.Error).Select(_ => _.Message).ToList();
            }
        }

        public List<string> LogWarnings
        {
            get
            {
                return Child_DatabaseItemLogs.Where(_ => _.MessageType == (int)DatabaseItemLogTypeEnum.Warning).Select(_ => _.Message).ToList();
            }
        }

        public List<string> LogInfos
        {
            get
            {
                return Child_DatabaseItemLogs.Where(_ => _.MessageType == (int)DatabaseItemLogTypeEnum.Info).Select(_ => _.Message).ToList();
            }
        }

        public bool IsColumnDuplicate(string name_sanitized)
        {
            return Child_Columns.Where(c => c.NameSanitized == name_sanitized).Count() > 1;
        }

        internal IEnumerable<ForeignKey> ParentForeignKeys
        {
            get
            {
                foreach (var c in Child_Columns) {
                    if (!c.ReferencedColumnId.HasValue)
                        continue;
                    // find table with referenced column id
                    var parentTable = Parent_DatabaseModel.Tables.First(_ => _.Child_Columns.Any(_2 => _2.Id == c.ReferencedColumnId));

                    if (!parentTable.IsIncluded)
                        continue;

                    // find parent column
                    var parentColumn = parentTable.Child_Columns.First(_ => _.Id == c.ReferencedColumnId);

                    yield return new ForeignKey(this, c, parentTable, parentColumn);
                }
            }
        }

        internal IEnumerable<ForeignKey> ChildForeignKeys
        {
            get
            {
                foreach (var c in Child_Columns) {
                    // find table that reference this column
                    var childTables = Parent_DatabaseModel.Tables.Where(_ => _.Child_Columns.Any(_2 => _2.ReferencedColumnId == c.Id));

                    foreach (var childTable in childTables) {
                        if (!childTable.IsIncluded)
                            continue;
                        var childColumn = childTable.Child_Columns.First(_ => _.ReferencedColumnId == c.Id);
                        yield return new ForeignKey(childTable, childColumn, this, c);
                    }
                }
            }
        }
    }
}
